import React, { useEffect, useState } from "react";
import { Space, Table, Card, Modal, Button, Drawer } from "antd";
import type { ColumnsType } from "antd/es/table";
import useModal from "@/hooks/useModal";
import useMessage from "@/hooks/useMessage";
import styles from "./index.module.less";
import MessageAdd from "../MessageAdd";
import { Message } from "@/api/interface/system";
import { deleteMessageApi, getMessageListApi } from "@/api/modules/system";
import MessageFilter from "../MessageFilter";

interface DataType {
	id: number;
	key: number;
	name: string;
	createdAt: string;
	updatedAt: string;
}
[];

const MessageList: React.FC = props => {
	const columns: ColumnsType<DataType> = [
		{
			title: "id",
			dataIndex: "id",
			key: "id"
		},
		{
			title: "内容",
			dataIndex: "content",
			key: "content"
		},
		{
			title: "创建日期",
			dataIndex: "createdAt",
			key: "createdAt"
		},
		{
			title: "修改日期",
			dataIndex: "updatedAt",
			key: "updatedAt"
		},
		{
			title: "操作",
			key: "action",
			render: (_, row) => {
				return (
					<>
						<Space size="middle">
							<a onClick={() => openEdit(row)}>修改</a>
							<a onClick={() => openModal(row.id)}>删除</a>
						</Space>
					</>
				);
			}
		}
	];

	const listExtra = (
		<div style={{ display: "flex", gap: 8 + "px" }}>
			<Button type="primary" onClick={() => setAddVisible(true)}>
				新增
			</Button>
			<Button type="primary" onClick={() => getMessageList()}>
				刷新
			</Button>
		</div>
	);

	const { contextHolder, success, error } = useMessage();
	const { id, isModalOpen, openModal, closeModal } = useModal();

	const [messageList, setMessageList] = useState<Message[]>([]);
	const [messageId, setMessageId] = useState<number>(0);

	const getMessageList = async (page: number = 1, size: number = 10, wrapper?: Partial<Message>) => {
		const res = await getMessageListApi(page, size, wrapper);
		if (res.code === 200) {
			setMessageList(res.data.rows);
			setTotal(res.data.total);
		}
	};

	const deleteMessage = async () => {
		const res = await deleteMessageApi(id);
		if (res.code === 200) {
			success("删除成功");
		} else {
			error("删除失败");
		}
		closeModal();
	};

	const data: any = messageList.map(item => {
		return {
			id: item.id,
			key: item.id,
			content: item.content,
			createdAt: item.createdAt,
			updatedAt: item.updatedAt
		};
	});

	useEffect(() => {
		getMessageList();
	}, []);

	// 修改
	const [editVisible, setEditVisible] = useState(false);
	const openEdit = async (message: DataType) => {
		setMessageId(message.id);
		setEditVisible(true);
	};
	const onCloseEdit = () => {
		getMessageList();
		setEditVisible(false);
	};

	// 新增
	const [addVisible, setAddVisible] = useState(false);
	const onCloseAdd = () => {
		getMessageList();
		setAddVisible(false);
	};

	const onChangePage = (page: number) => {
		getMessageList(page, 10);
	};
	const [total, setTotal] = useState<number>(0);

	// 搜索
	const onSearch = (data?: Message) => {
		getMessageList(1, 10, data);
	};

	return (
		<div className={styles.root}>
			{contextHolder}
			<MessageFilter onSearch={onSearch} />
			<Card title="数据列表" extra={listExtra}>
				<Table columns={columns} dataSource={data} pagination={{ total: total, onChange: onChangePage }} />
			</Card>

			<Modal title="提醒" mask={false} visible={isModalOpen} onOk={deleteMessage} onCancel={closeModal}>
				确定删除该消息吗
			</Modal>

			<Drawer
				title="新增"
				placement="right"
				width={500}
				onClose={() => {
					setAddVisible(false);
				}}
				visible={addVisible}
			>
				<MessageAdd onComfirm={onCloseAdd}></MessageAdd>
			</Drawer>

			<Drawer
				title="修改"
				placement="right"
				width={500}
				onClose={() => {
					setEditVisible(false);
				}}
				visible={editVisible}
			>
				<MessageAdd type="edit" messageId={messageId} onComfirm={onCloseEdit}></MessageAdd>
			</Drawer>
		</div>
	);
};

export default MessageList;
